package com.qkl.xiatang.app.web.controller;

import com.qkl.xiatang.app.dto.device.SensorStatisticsInDTO;
import com.qkl.xiatang.app.dto.device.SensorTypeSortWebDTO;
import com.qkl.xiatang.app.service.impl.DeviceManagerService;
import com.qkl.xiatang.app.utils.AppUtils;
import com.qkl.xiatang.app.utils.ErrorCodeUtils;
import com.qkl.xiatang.app.utils.Result;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.ObjectUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("{app.baseURL}")
@Slf4j
public class DeviceManagerController {

    @Autowired
    private DeviceManagerService deviceManagerService;


    /**
     * 传感器设备网关查询
     * @return
     */
    @GetMapping("/querySensorList")
    public Result querySensorList() {
        return  ControllerHelper.toQueryResult(deviceManagerService.querySensorList());
    }


    /**
     * 传感器设备网关设置默认
     * @param params
     * @return
     */
    @PostMapping("/sensorDefault")
    public Result sensorDefault(@RequestBody Map<String,Object> params) {
        String sensorId = StringUtils.trimToEmpty((String) params.get("sensorId"));
        if(ObjectUtils.isEmpty(sensorId)){
            return Result.error(ErrorCodeUtils.PARAM_EMPTY, ErrorCodeUtils.PARAM_EMPTY_MSG);
        }
        return ControllerHelper.toOperateResult(deviceManagerService.sensorDefault(sensorId));
    }

    /**
     * 传感器网关数据查询
     * @param params
     * @return
     */
    @GetMapping("/querySensorData")
    public Result querySensorData(@RequestParam Map<String,Object> params) {
        String sn = StringUtils.trimToEmpty((String) params.get("sn"));
        return ControllerHelper.toQueryResult(deviceManagerService.querySensorData(sn));
    }

    /**
     * 传感器类型查询
     * @return
     */
    @GetMapping("/querySensorTypeList")
    public Result querySensorTypeList(@RequestParam Map<String,Object> params) {
        String sensorId = StringUtils.trimToEmpty((String) params.get("sensorId"));
        return ControllerHelper.toQueryResult(deviceManagerService.querySensorTypeList(sensorId));
    }

    /**
     * 传减器数据统计
     * @param params
     * @return
     */
    @GetMapping("/querySensorStatistics")
    public Result querySensorStatistics(@RequestParam Map<String,Object> params) {
        String sensorTypeId = StringUtils.trimToEmpty((String) params.get("sensorTypeId"));
        String statisticsType = StringUtils.trimToEmpty((String) params.get("statisticsType"));
        String startTime = StringUtils.trimToEmpty((String) params.get("startTime"));
        String endTime = StringUtils.trimToEmpty((String) params.get("endTime"));
        log.info("==querySensorStatistics.sensorTypeId={},startTime={},endTime={}",sensorTypeId,startTime,endTime);
        if(StringUtils.isAnyEmpty(sensorTypeId,statisticsType)){
            return Result.error(ErrorCodeUtils.PARAM_EMPTY, ErrorCodeUtils.PARAM_EMPTY_MSG);
        }
        SensorStatisticsInDTO dto = SensorStatisticsInDTO.parseParams(sensorTypeId,startTime,endTime);
        dto.setStatisticsType(statisticsType);
        if(null == dto.getSensorType() || StringUtils.isEmpty(dto.getSensorId()) || !dto.checkStatisticsType()){
            return Result.error(ErrorCodeUtils.PARAM_ILLEGAL, ErrorCodeUtils.PARAM_ILLEGAL_MSG);
        }
        return ControllerHelper.toQueryResult(deviceManagerService.querySensorStatistics(dto));
    }

    /**
     * 传感器、塘口绑定
     * @param params
     * @return
     */
    @PostMapping("/bindSensorPond")
    public Result bindSensorPond(@RequestBody Map<String,Object> params) {
        String sensorId = StringUtils.trimToEmpty((String) params.get("sensorId"));
        String pondId = StringUtils.trimToEmpty((String) params.get("pondId"));
        if(StringUtils.isAnyEmpty(sensorId,pondId)){
            return Result.error(ErrorCodeUtils.PARAM_EMPTY, ErrorCodeUtils.PARAM_EMPTY_MSG);
        }
        return ControllerHelper.toOperateResult(deviceManagerService.bindSensorPond(sensorId,pondId));
    }


    /**
     * 传感器类型排序
     * @param webDTO
     * @return
     */
    @PostMapping("/sensorTypeSort")
    public Result sensorTypeSort(@RequestBody SensorTypeSortWebDTO webDTO) {
        String sensorId = StringUtils.trimToEmpty(webDTO.getSensorId());
        List<Integer> sort = AppUtils.integersDistinct(webDTO.getSort());
        if(StringUtils.isEmpty(sensorId) || ObjectUtils.isEmpty(sort)){
            return Result.error(ErrorCodeUtils.PARAM_EMPTY, ErrorCodeUtils.PARAM_EMPTY_MSG);
        }
        SensorTypeSortWebDTO dto = new SensorTypeSortWebDTO();
        dto.setSensorId(sensorId);
        dto.setSort(sort);
        return ControllerHelper.toOperateResult(deviceManagerService.sensorTypeSort(dto));
    }

}
